﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using WMSABL;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message;
using System.Threading;

namespace ElectronicWMSA.SY.SystemDocking
{
    public partial class SAPDockingFrm : Form
    {
        DataSet dsSAP = new DataSet();
        DataSet dsEWMSA = new DataSet();
        PublicDataSet bll_EWMSA = new PublicDataSet();
        PublicDataSet_SAP bll_SAP = new PublicDataSet_SAP();
        public SAPDockingFrm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 系统用户
        /// </summary>
        private void btnUserInfo_Click(object sender, EventArgs e)
        {
            WaitingBoxFrm wf = new WaitingBoxFrm((obj, args) => { Thread.Sleep(1000); UserInfo(); }, 30); wf.ShowDialog(this);
        }
        private void UserInfo()
        {
            dsSAP = new DataSet();
            dsSAP = bll_SAP.GetDataSet("SELECT '18' Organization_id,UserCode AS Login_name,UserName AS Username,'670B14728AD9902AECBA32E22FA4F6BD' AS password,getdate() AS Creat_time,LastOnlineTime AS Last_login_time,'0' AS Count,'1' AS EnableStatus,'0' AS LoginStatus FROM BS4_User  ");
            if (dsSAP != null && dsSAP.Tables[0].Rows.Count > 0)
            {
                if (MessageClass.enquire("确认导入用户信息？") == DialogResult.OK)
                {
                    bll_EWMSA.ExecuteSQL(" TRUNCATE TABLE TUsersSAP ");
                    if (!bll_EWMSA.DataTableToSQLUserSAPList(dsSAP.Tables[0]))
                    {
                        MessageClass.warning("导入用户信息失败！");
                        return;
                    }
                    dsEWMSA = new DataSet();
                    dsEWMSA = bll_EWMSA.GetDataSet(" SELECT  Organization_id,  Login_name,Username, password, Creat_time, Last_login_time, Count, EnableStatus, LoginStatus FROM TUsersSAP  WHERE Login_name NOT IN (SELECT Login_name FROM TUsers) ");
                    if (dsEWMSA != null && dsEWMSA.Tables[0].Rows.Count > 0)
                    {
                        if (!bll_EWMSA.DataTableToSQLUserList(dsEWMSA.Tables[0]))
                        {
                            MessageClass.warning("导入用户信息失败！");
                        }
                        else
                        {
                            MessageClass.right("操作成功！");
                        }
                        bll_EWMSA.ExecuteSQL(" TRUNCATE TABLE TUsersSAP ");
                    }
                    else
                    {
                        MessageClass.warning("没有需要导入的数据！");
                    }
                }
            }
            else
            {
                MessageClass.warning("没有需要导入的用户信息！");
            }
        }
        /// <summary>
        /// 仓位信息
        /// </summary>
        private void btnPostion_Click(object sender, EventArgs e)
        {
            WaitingBoxFrm wf = new WaitingBoxFrm((obj, args) => { Thread.Sleep(1000); PostionInfo(); }, 30); wf.ShowDialog(this);
        }
        private void PostionInfo()
        {
            dsSAP = new DataSet();
            dsSAP = bll_SAP.GetDataSet(" SELECT DISTINCT WarehouseNo AS WarehouseNO,  SUBSTRING(Closet,0,3)  AS AreaNO, Closet AS PositionNO, Closet AS PositionName, '1' AS Stauts,  '未分配' AS Designated_Stauts from    PD11_Closet  where WarehouseNo!='' and Closet!=''  order by Closet ");
            if (dsSAP != null && dsSAP.Tables[0].Rows.Count > 0)
            {
                if (MessageClass.enquire("确认导入仓位信息？") == DialogResult.OK)
                {
                    bll_EWMSA.ExecuteSQL(" TRUNCATE TABLE PD_PositionSAP ");
                    System.Threading.Thread.Sleep(500);
                    if (!bll_EWMSA.DataTableToSQLPositionSAPList(dsSAP.Tables[0]))
                    {
                        MessageClass.warning("导入仓位信息失败！");
                        return;
                    }
                    System.Threading.Thread.Sleep(500);
                    dsEWMSA = new DataSet();
                    dsEWMSA = bll_EWMSA.GetDataSet(" SELECT WarehouseNO,  AreaNO, PositionNO,PositionName, Stauts,  Designated_Stauts  FROM PD_PositionSAP WHERE PositionNO NOT IN (SELECT PositionNO FROM PD_Position) ");
                    if (dsEWMSA != null && dsEWMSA.Tables[0].Rows.Count > 0)
                    {
                        if (!bll_EWMSA.DataTableToSQLPositionList(dsEWMSA.Tables[0]))
                        {
                            MessageClass.warning("导入仓位信息失败！");
                        }
                        else
                        {
                            MessageClass.right("操作成功！");
                        }
                        bll_EWMSA.ExecuteSQL(" TRUNCATE TABLE PD_PositionSAP ");
                    }
                    else
                    {
                        MessageClass.warning("没有需要导入的数据！");
                    }
                }
            }
            else
            {
                MessageClass.warning("没有需要导入的数据！");
            }
        }

        /// <summary>
        /// 库存
        /// </summary>
        private void btnStock_Click(object sender, EventArgs e)
        {
            WaitingBoxFrm wf = new WaitingBoxFrm((obj, args) => { Thread.Sleep(1000); StockInfo(); }, 30); wf.ShowDialog(this);
        }
        private void StockInfo()
        {
            dsSAP = new DataSet();
            dsSAP = bll_SAP.GetDataSet(" Select  A.WarehouseNo as WarehouseNO,A.Closet as PositionNO,A.ProductNo as ProductNO, A.ProductName as ProductName,A.Qty as Quantity,A.Unit as Unit From ST01_PID A   ");
            if (dsSAP != null && dsSAP.Tables[0].Rows.Count > 0)
            {
                if (MessageClass.enquire("确认导入库存信息？") == DialogResult.OK)
                {
                    bll_EWMSA.ExecuteSQL(" TRUNCATE TABLE PD_StockSAP ");
                    if (!bll_EWMSA.DataTableToSQLStockSAPList(dsSAP.Tables[0]))
                    {
                        MessageClass.warning("导入库存信息失败！");
                        return;
                    }
                    dsEWMSA = new DataSet();
                    dsEWMSA = bll_EWMSA.GetDataSet(" SELECT WarehouseNO,  PositionNO, ProductNO,ProductName, Quantity,  Unit FROM PD_StockSAP WHERE ProductNO NOT IN (SELECT ProductNO FROM PD_Stock) ");
                    if (dsEWMSA != null && dsEWMSA.Tables[0].Rows.Count > 0)
                    {
                        if (!bll_EWMSA.DataTableToSQLStockList(dsEWMSA.Tables[0]))
                        {
                            MessageClass.warning("导入库存信息失败！");
                        }
                        else
                        {
                            MessageClass.right("操作成功！");
                        }
                        bll_EWMSA.ExecuteSQL(" TRUNCATE TABLE PD_StockSAP ");
                    }
                    else
                    {
                        MessageClass.warning("没有需要导入的数据！");
                    }
                }
            }
            else
            {
                MessageClass.warning("没有需要导入的数据！");
            }
        }
        /// <summary>
        /// 部品信息（供应商）
        /// </summary>
        private void btnProductNO_Click(object sender, EventArgs e)
        {
            WaitingBoxFrm wf = new WaitingBoxFrm((obj, args) => { Thread.Sleep(1000); ProductNOInfo(); }, 30); wf.ShowDialog(this);
        }
        private void ProductNOInfo()
        {
            dsSAP = new DataSet();
            dsSAP = bll_SAP.GetDataSet("select 	a.BillNo as GIT_NO,b.VendorCode,a.PU02BillNo as PO_NO,a.InvoiceNo as Invoice_No,a.OrderDate,a.ProductNo,a.ProductName,a.Specification,a.Unit,a.QTY,a.Currency,a.Price,a.Weight,a.HGSpecification from PU02_Order a left outer join PD01_Product b on(a.ProductNo=b.ProductNo and b.State=1) ");
            if (dsSAP != null && dsSAP.Tables[0].Rows.Count > 0)
            {
                if (MessageClass.enquire("确认导入部品信息？") == DialogResult.OK)
                {
                    bll_EWMSA.ExecuteSQL(" TRUNCATE TABLE PD_ProductOrderSAP ");
                    if (!bll_EWMSA.DataTableToSQLProductOrderASPList(dsSAP.Tables[0]))
                    {
                        MessageClass.warning("导入部品信息失败！");
                        return;
                    }
                    dsEWMSA = new DataSet();
                    dsEWMSA = bll_EWMSA.GetDataSet(" SELECT GIT_NO,VendorCode,PO_NO,Invoice_No,OrderDate,ProductNo,ProductName,Specification,Unit,QTY,Currency,Price,Weight,HGSpecification FROM PD_ProductOrderSAP WHERE GIT_NO NOT IN (SELECT GIT_NO FROM PD_ProductOrder) ");
                    if (dsEWMSA != null && dsEWMSA.Tables[0].Rows.Count > 0)
                    {
                        if (!bll_EWMSA.DataTableToSQLProductOrderList(dsEWMSA.Tables[0]))
                            MessageClass.warning("导入部品信息失败！");
                        else
                            MessageClass.right("操作成功！");
                        bll_EWMSA.ExecuteSQL(" TRUNCATE TABLE PD_ProductOrderSAP ");
                    }
                    else
                    {
                        MessageClass.warning("没有需要导入的数据！");
                    }
                }
            }
            else
            {
                MessageClass.warning("没有需要导入的数据！");
            }
        }

        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }

        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }

        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }

    }
}
